package com.jay.media.music.ui.file

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jay.base.ui.BaseFragment
import com.jay.media.kit.LogHelper
import com.jay.media.music.ui.R
import java.io.File
import java.util.*
import kotlin.collections.ArrayList

/**
 * @author xuzhou4
 * @date 2021/12/22
 */
class FileBrowserFragment : BaseFragment() {

    companion object {
        const val KEY_PATH: String = "file_path"
        fun newInstance(dir: String): FileBrowserFragment {
            val fileBrowserFragment = FileBrowserFragment()
            val bundle = Bundle()
            bundle.putString(KEY_PATH, dir)
            fileBrowserFragment.arguments = bundle
            return fileBrowserFragment
        }
    }

    private lateinit var mRecyclerView: RecyclerView
    private lateinit var mDirPath: String
    private var mFileList: List<File> = ArrayList()
    private var mOnFileItemClickListener: OnFileItemClickListener? = null

    fun setOnFileItemClickListener(onFileItemClickListener: OnFileItemClickListener?) {
        this.mOnFileItemClickListener = onFileItemClickListener
    }

    override fun layoutId(): Int = R.layout.fragment_file_browser

    override fun initParams() {
        mDirPath = arguments?.getString(KEY_PATH) ?: ""
    }

    override fun initView(view: View) {
        LogHelper.d(TAG, "initView")

        val file = File(mDirPath)
        val list = file.listFiles()
        mFileList = list?.toList() as List<File>

        Collections.sort(mFileList, FileComparator())

        mRecyclerView = requireView().findViewById(R.id.recyclerView)
        mRecyclerView.layoutManager = LinearLayoutManager(mActivity)
        mRecyclerView.adapter = FileAdapter()
    }

    private inner class FileAdapter : RecyclerView.Adapter<FileAdapter.Holder>() {

        inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var mIVIcon: ImageView = itemView.findViewById(R.id.iv_icon)
            var mIVMenu: ImageView = itemView.findViewById(R.id.iv_menu)
            var mTVName: TextView = itemView.findViewById(R.id.tv_name)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
            val itemView = layoutInflater.inflate(R.layout.item_file_layout, null)
            return Holder(itemView)
        }

        override fun onBindViewHolder(holder: Holder, position: Int) {
            holder.mIVIcon.setImageResource(if (mFileList[position].isDirectory) R.drawable.ic_directory else R.drawable.ic_file)
            holder.mTVName.text = mFileList[position].name
            holder.mIVMenu.visibility =
                if (mFileList[position].isDirectory) View.GONE else View.VISIBLE
            holder.itemView.setOnClickListener {
                mOnFileItemClickListener?.onItemClick(mFileList[position], it)
            }
            holder.mIVMenu.setOnClickListener {
                mOnFileItemClickListener?.onItemMenuClick(mFileList[position], it)
            }
        }

        override fun getItemCount(): Int {
            return mFileList.size
        }
    }

    private class FileComparator : Comparator<File> {
        override fun compare(o1: File?, o2: File?): Int {
            return (o1?.name ?: "").compareTo(o2?.name ?: "")
        }
    }
}